import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.LinkedList;

public class MDUser extends JPanel implements ActionListener
{

    public JTextField   message;
    public JTextArea    serverText;
    private Socket       sock;                   
    public PrintWriter  remoteOut;
    private String       hostname;
    private int          port = 8888;
    public ReceivingThread rt;
    private String fileName = "";
    protected JComboBox usernameBox;
    protected JButton passControl;
    public MultiDraw2 md;
    public boolean ownerreceived = false;
    public LinkedList<ShapeObject> ShapeObjects = null;
   
    public MDUser(MultiDraw2 md) 
    {
    	this.md = md;
    	setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    	System.out.println("Please type a hostname or type local");
        hostname = md.hostname;

        Font fp = new Font("Serif", Font.PLAIN, 12);
        Font fb = new Font("SansSerif", Font.BOLD, 12);
        
        JLabel received = new JLabel("Text received: ");
        received.setFont(fb);
        this.add(received);
        
        serverText = new JTextArea(40, 20); 
        serverText.setFont(fb);
        serverText.setLineWrap(true);
        JScrollPane sp = new JScrollPane(serverText);
        sp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        sp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        this.add(sp);
        
        JLabel text = new JLabel("Text to send: ");
        text.setFont(fb);
        this.add(text);
        
        //message = new JTextField(30);
        message = new JTextField(20);
        message.addActionListener(this);
        this.add(message);     

        JPanel userPanel = new JPanel();
        userPanel.add(new JLabel("Pass control to: "));
        usernameBox = new JComboBox();
        userPanel.add(usernameBox);

        passControl = new JButton("Submit");
        passControl.setEnabled(true);
        userPanel.add(passControl);
        passControl.addActionListener(this);
        add(userPanel);
        
        this.client();
    }
    public boolean loadCurrentUsers(String users)
    {
    	System.out.println(users);
 	   String[] userList = users.split(" ");
 	   DefaultComboBoxModel theModel = (DefaultComboBoxModel)usernameBox.getModel();
 	   theModel.removeAllElements();
 	   for(int i = 0; i < userList.length - 2; i++)
 	   {
 		   usernameBox.addItem(userList[i+2]);
 		   if(i == 0)
 		   {
 			   usernameBox.setSelectedIndex(0);
 		   }
 	   }
 	   if(usernameBox.getItemCount() > 0)
 		   return(true);
 	   else
 		   return(false);
    }
    public void sendShapeObjects(){
    	
    	ObjectOutputStream oos;
		try {
			oos = new ObjectOutputStream(sock.getOutputStream());
			oos.writeObject(md.ShapeObjects);
		} catch (IOException e) {
			e.printStackTrace();
		}
    }
   private void client() 
   {
	   sock = md.sock;
      try 
      {  
    	  if (hostname.equals("local")) hostname = null;

         InetAddress serverAddr = InetAddress.getByName(hostname);
         remoteOut = new PrintWriter(sock.getOutputStream(),true);
         System.out.println("Connected to server " +
            serverAddr.getHostName() + " on port " + sock.getPort());
         rt = new ReceivingThread(this,sock);
         rt.start();
      } catch (IOException e) 
      { System.out.println(e.getMessage() + ": Failed to connect to server."); }
   }

   public void actionPerformed(ActionEvent e) 
   {
       if (e.getSource() == message) 
       {  String s = message.getText().trim();
          remoteOut.println("<<USRMSG>>"+s+"<<USRMSG>>");
          message.setText("");   
          if (s.equals("bye")) 
          {
        	  cleanUp();
        	  System.exit(0); 
          }
       }else if(e.getSource() == passControl)
       {
   	  	if(!md.userName.equals((String)usernameBox.getSelectedItem()))
   		{
	        	  System.out.println("Sending new owner..");
	        	  String newControlOwner = (String)usernameBox.getSelectedItem();
	        	  remoteOut.println("<<OWNR>> " + newControlOwner);
	        	  System.out.println("Sent new owner..");
   		}
   	  	else
   	  	{
   	  		JOptionPane.showMessageDialog(this, "Hey! You are the current owner of control.");
   	  	}
      }
   }

   void cleanUp()
   {
       try 
       {   if (sock != null) sock.close(); 
       } 
       catch (IOException x) { }
   }
   public String getFileName(){
	   return this.fileName;
   }
   public void setFileName(String fileName){
	   this.fileName = fileName;
   }
}
